//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.Generic;
using OpenLS.Core.Serialization;
using OpenLS.Spreadsheet;
using OpenLS.Spreadsheet.Data;

namespace OpenLS.Spreadsheet.Pivot
{
    class CacheField : IOfficeXmlSerializable
    {
        private string _name;
        string _caption;
        string _propertyName;
        Boolean _serverField;
        Boolean _uniqueList;
        int? _numFmtId;
        string _formula;
        Int32 _sqlType;
        Int32 _hierarchy;
        UInt32 _level;
        Boolean _databaseField;
        UInt32? _mappingCount;
        Boolean _memberPropertyField;
        SharedItems _sharedItems;
        FieldGroup _fieldGroup;
        List<Index> _mpMap = new List<Index>();
        ExtensionList _extLst;
        private string xmlName = "cacheField";

        public string Name
        {
            get { return _name; }
        }

        public void ReadXml(ReadContext context)
        {
            using (var c = context.Read(xmlName))
            {
                this._name = c.GetString("name");
                this._caption = c.GetOptionalString("caption");
                this._propertyName = c.GetOptionalString("propertyName");
                _serverField = c.GetOptionalBoolean("serverField", false);
                _uniqueList = c.GetOptionalBoolean("uniqueList", true);
                this._numFmtId = c.GetOptionalInteger("numFmtId");
                this._formula = c.GetOptionalString("formula");
                _sqlType = c.GetOptionalInteger("sqlType", 0);
                _hierarchy = c.GetOptionalInteger("hierarchy", 0);
                _level = c.GetOptionalUInteger("level", 0);
                _databaseField = c.GetOptionalBoolean("databaseField", true);
                _mappingCount = c.GetOptionalUInteger("mappingCount");
                _memberPropertyField = c.GetOptionalBoolean("memberPropertyField", false);
                _sharedItems = c.ReadOptionalElement<SharedItems>("sharedItems");
                this._fieldGroup = c.ReadOptionalElement<FieldGroup>("fieldGroup");
                _mpMap.Clear();
                _mpMap.AddRange(c.ReadOptionalWhile<Index>("mpMap"));
                _extLst = c.ReadOptionalElement<ExtensionList>("extLst");
            }
        }

        public void WriteXml(WriteContext context)
        {
            using (var c = context.Write(xmlName))
            {
                c.SetString("name", Name);
                c.SetOptionalString("caption", _caption);
                c.SetOptionalString("propertyName", _propertyName);
                c.SetOptionalBoolean("serverField", _serverField, false);
                c.SetOptionalBoolean("uniqueList", _uniqueList, true);
                c.SetOptionalInteger("numFmtId", _numFmtId);
                c.SetOptionalString("formula", _formula);
                c.SetOptionalInteger("sqlType", _sqlType, 0);
                c.SetOptionalInteger("hierarchy", _hierarchy, 0);
                c.SetOptionalUInteger("level", _level, 0);
                c.SetOptionalBoolean("databaseField", _databaseField, true);
                c.SetOptionalUInteger("mappingCount", _mappingCount);
                c.SetOptionalBoolean("memberPropertyField", _memberPropertyField, false);
                c.WriteOptionalElement(_sharedItems);
                c.WriteOptionalElement(_fieldGroup);
                c.WriteElements(_mpMap);
                c.WriteOptionalElement(_extLst);

            }
        }

        internal object GetValue(PivotTableValue value)
        {
            if (value is Index)
            {
                Index i = (Index) value;
                value =  this._sharedItems[(int)i.Value];
            }
            return value.Value;
        }
    }

    internal class FieldGroup : IOfficeXmlSerializable
    {
        public void ReadXml(ReadContext context)
        {
            throw new NotImplementedException();
        }

        public void WriteXml(WriteContext context)
        {
            throw new NotImplementedException();
        }
    }
}
